package com.klb.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.klb.base.DaoBase;
import com.klb.dao.ArticleDao;
import com.klb.model.BbsArticle;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/6/5.
 */
public class ArticleDaoImpl extends DaoBase implements ArticleDao {

    /**
     * 根据id获取文章信息
     * */
    @Override
    public BbsArticle getArticleById(int articleId) {
    	BbsArticle article = null;
    	
        if (!Tools.objectIsNullOrNot(articleId)){
            Session session = this.hibernateSessionFactory.getCurrentSession();
            Transaction tx = null;
            String sql = "from BbsArticle article where article.articleId = :articleId";

            try {

                tx = session.beginTransaction();

                Query query = session.createQuery(sql);
                query.setParameter("articleId", articleId);

                article = (BbsArticle) query.uniqueResult();

                tx.commit();

            } catch (Exception e){
                tx.rollback();
                e.printStackTrace();
            }

        }
        return article;
    }

    /**
     * 获取所有文章
     * */
	@Override
    public List<BbsArticle> getAllArticles() {
    	List<BbsArticle> lists = null;
    	
        Session session = this.hibernateSessionFactory.getCurrentSession();
        Transaction tx = null;
        String sql = "from BbsArticle";

        try {

            tx = session.beginTransaction();

            Query query = session.createQuery(sql);

            lists = query.list();

            tx.commit();

        } catch (Exception e){
            tx.rollback();
            e.printStackTrace();
        }
        
        return lists;
    }

	

    /**
     * 根据版区id获取文章列表
     * */
    @Override
    public List<BbsArticle> getArticlesByBlockId(int blockId) {
    	List<BbsArticle> lists = null;
    	
        if (!Tools.objectIsNullOrNot(blockId)){
            Session session = this.hibernateSessionFactory.getCurrentSession();
            Transaction tx = null;
            String sql = "from BbsArticle article where article.blockId = :blockId";

            try {

                tx = session.beginTransaction();

                Query query = session.createQuery(sql);
                query.setParameter("blockId", blockId);

                lists = query.list();

                tx.commit();

            } catch (Exception e){
                tx.rollback();
                e.printStackTrace();
            }

        }
        
        return lists;
    }


    /**
     * 根据文章热度获取文章列表
     * */
    @Override
    public List<BbsArticle> getHotArticles() {
    	List<BbsArticle> lists = null;
    	
        Session session = this.hibernateSessionFactory.getCurrentSession();
        Transaction tx = null;

        try {
            String sql = "from BbsArticle article order by article.praiseNumber asc";
            tx = session.beginTransaction();

            Query query = session.createQuery(sql).setMaxResults(6);

            lists = query.list();

            tx.commit();

        }catch (Exception e){
            tx.rollback();
            e.printStackTrace();
        }

        return lists;
    }


    /**
     * 获取最新文章
     * */
    @Override
    public List<BbsArticle> getNewArticles() {
    	List<BbsArticle> lists = null;
    	
        Session session = this.hibernateSessionFactory.getCurrentSession();
        Transaction tx = null;

        try {
            String sql = "from BbsArticle article order by article.articleId desc";
            tx = session.beginTransaction();

            Query query = session.createQuery(sql).setMaxResults(6);

            lists = query.list();

            tx.commit();

        } catch (Exception e){
            tx.rollback();
            e.printStackTrace();
        }

        return lists;
    }


    /**
     *  根据articleId增加评论数：
     *  1、通过articleId获取指定BbsArticles类
     *  2、该类评论数加一
     *  3、更新数据
     * */
    @Override
    public boolean addCommentByArticleId(int articleId) {
        if (!Tools.objectIsNullOrNot(articleId)){
            Session session = this.hibernateSessionFactory.getCurrentSession();
            Transaction tx = null;
            String sql = "from BbsArticle article where article.articleId = :articleId";

            try {

                tx = session.beginTransaction();

                Query query = session.createQuery(sql);
                query.setParameter("articleId", articleId);

                BbsArticle article = (BbsArticle) query.uniqueResult();

                if (!Tools.objectIsNullOrNot(article)){
                    int comment = article.getCommentNumber() + 1;
                    article.setCommentNumber(comment);
                    session.saveOrUpdate(article);
                    tx.commit();
                    return true;
                } else {
                    tx.rollback();
                }

            } catch (Exception e){
                tx.rollback();
                e.printStackTrace();
            }
        }

        return false;
    }

    
    /**
     * 根据用户id获取文章列表
     * */
    @Override
    public List<BbsArticle> getArticlesByUserId(int userId) {
    	List<BbsArticle> lists = null;
    	
        if (!Tools.objectIsNullOrNot(userId)){
            Session session = this.hibernateSessionFactory.getCurrentSession();
            Transaction tx = null;
            String sql = "from BbsArticle article where article.userId = :userId";

            try {

                tx = session.beginTransaction();

                Query query = session.createQuery(sql);
                query.setParameter("userId", userId);

                lists = query.list();

                tx.commit();

            } catch (Exception e){
                tx.rollback();
                e.printStackTrace();
            }

        }
        
        return lists;
    }
}
